<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Using Freemarker Templates</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="guides.html">Guides</a>&nbsp;&gt;&nbsp;<a href="user-guide.html">User Guide</a>&nbsp;&gt;&nbsp;<a href="using-freemarker-templates.html">Using Freemarker Templates</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Using Freemarker Templates</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=45128">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=45128">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=45128">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=45128">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=45128">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=45128">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><p><a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/" rel="nofollow">FreeMarker</a> is a Java-based template engine that is a great alternative to <a shape="rect" href="jsp.html">JSP</a>. FreeMarker is ideal for situations where your action results can possibly be loaded from outside a Servlet container. For example, if you wished to support plugins in your application, you might wish to use FreeMarker so that the plugins could provide the entire action class and view in a single jar that is loaded from the classloader.</p>

<style type="text/css">/*<![CDATA[*/
div.rbtoc1440489164885 {padding: 0px;}
div.rbtoc1440489164885 ul {list-style: disc;margin-left: 0px;}
div.rbtoc1440489164885 li {margin-left: 0px;padding-left: 0px;}

/*]]>*/</style><div class="toc-macro rbtoc1440489164885">
<ul class="toc-indentation"><li><a shape="rect" href="#UsingFreemarkerTemplates-Configureyouractiontousethe&quot;freemarker&quot;resulttype">Configure your action to use the "freemarker" result type</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-Usingproperties">Using properties</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-Servlet/JSPScopedObjects">Servlet / JSP Scoped Objects</a>
<ul class="toc-indentation"><li><a shape="rect" href="#UsingFreemarkerTemplates-ApplicationScopeAttribute">Application Scope Attribute</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-SessionScopeAttribute">Session Scope Attribute</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-RequestScopeAttribute">Request Scope Attribute</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-RequestParameter">Request Parameter</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-Contextparameter">Context parameter</a></li></ul>
</li><li><a shape="rect" href="#UsingFreemarkerTemplates-TemplateLoading">Template Loading</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-VariableResolution">Variable Resolution</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-FreeMarkerconfiguration">FreeMarker configuration</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-Tags">Tags</a>
<ul class="toc-indentation"><li><a shape="rect" href="#UsingFreemarkerTemplates-UsingStrutstags">Using Struts tags</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-UsingJSPtags">Using JSP tags</a></li></ul>
</li><li><a shape="rect" href="#UsingFreemarkerTemplates-TipsandTricks">Tips and Tricks</a>
<ul class="toc-indentation"><li><a shape="rect" href="#UsingFreemarkerTemplates-TypeConversionandLocales">Type Conversion and Locales</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-StringandNonStringValuesontags">String and Non String Values on tags</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-Extending">Extending</a></li></ul>
</li><li><a shape="rect" href="#UsingFreemarkerTemplates-FreeMarkeralternativesyntax">FreeMarker alternative syntax</a></li><li><a shape="rect" href="#UsingFreemarkerTemplates-FreeMarkerlinks">FreeMarker links</a></li></ul>
</div>

<h2 id="UsingFreemarkerTemplates-Configureyouractiontousethe&quot;freemarker&quot;resulttype">Configure your action to use the "freemarker" result type</h2>

<p>Struts provides a result type "freemarker" which renders a FreeMarker template. This result type is defined in <code>struts-default.xml</code>. To create pages using FreeMarker, set the result type of the actions to "freemarker".</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;include file="struts-default.xml"/&gt;
...
&lt;action name="test" class="package.Test"&gt;
  &lt;result type="freemarker"&gt;/WEB-INF/views/testView.ftl&lt;/result&gt;
&lt;/action&gt;
...
</pre>
</div></div>


<h2 id="UsingFreemarkerTemplates-Usingproperties">Using properties</h2>

<p>FreeMarker uses the ${...} notation to access properties. They are called interpolations. Properties on the actions (getter methods) will automatically be available on the FreeMarker templates. If an action has a "getName()" method, then its value can be inserted on the template like:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
Your name is: ${name}
</pre>
</div></div>

<div class="confluence-information-macro confluence-information-macro-note"><p class="title">Be Careful</p><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
<p>By default,  FreeMarker will throw an error if it finds a variable that is not defined, or has a null value. See this <a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/docs/app_faq.html#faq_picky_about_missing_vars" rel="nofollow">FAQ</a> for details.</p></div></div>


<h2 id="UsingFreemarkerTemplates-Servlet/JSPScopedObjects">Servlet / JSP Scoped Objects</h2>

<p>The following are ways to obtained Application scope attributes, Session scope attributes, Request scope attributes, Request parameters, and framework Context scope parameters:-</p>

<h3 id="UsingFreemarkerTemplates-ApplicationScopeAttribute">Application Scope Attribute</h3>

<p>Assuming there's an attribute with name <code>myApplicationAttribute</code> in the Application scope.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;#if Application.myApplicationAttribute?exists&gt;
     ${Application.myApplicationAttribute}
&lt;/#if&gt;
</pre>
</div></div>
<p>or</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.property value="%{#application.myApplicationAttribute}" /&gt;
</pre>
</div></div>

<h3 id="UsingFreemarkerTemplates-SessionScopeAttribute">Session Scope Attribute</h3>

<p>Assuming there's an attribute with name <code>mySessionAttribute</code> in the Session scope.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;#if Session.mySessionAttribute?exists&gt;
     ${Session.mySessionAttribute}
&lt;/#if&gt;
</pre>
</div></div>
<p>or</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.property value="%{#session.mySessionAttribute}" /&gt;
</pre>
</div></div>

<h3 id="UsingFreemarkerTemplates-RequestScopeAttribute">Request Scope Attribute</h3>

<p>Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;#if Request.myRequestAttribute?exists&gt;
      ${Request.myRequestAttribute}
&lt;/#if&gt;
</pre>
</div></div>
<p>or</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.property value="%{#request.myRequestAttribute}" /&gt;
</pre>
</div></div>

<h3 id="UsingFreemarkerTemplates-RequestParameter">Request Parameter</h3>

<p>Assuming there's a request parameter myParameter (eg. <a shape="rect" class="external-link" href="http://host/myApp/myAction.action?myParameter=one" rel="nofollow">http://host/myApp/myAction.action?myParameter=one</a>).</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;#if Parameters.myParameter?exists&gt;
     ${Parameters.myParameter}
&lt;/#if&gt;
</pre>
</div></div>
<p>or</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.property value="%{#parameters.myParameter}" /&gt;
</pre>
</div></div>

<h3 id="UsingFreemarkerTemplates-Contextparameter">Context parameter</h3>

<p>Assuming there's a parameter with the name myContextParam in framework context.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
${stack.findValue('#myContextParam')}
</pre>
</div></div>
<p>or</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.property value="%{#myContextParam}" /&gt;
</pre>
</div></div>

<h2 id="UsingFreemarkerTemplates-TemplateLoading">Template Loading</h2>

<p>The framework looks for FreeMarker templates in two locations (in this order):</p>
<ol><li>Web application</li><li>Class path</li></ol>


<p>This ordering makes it ideal for providing templates inside a fully-built jar, but allowing for overrides of those templates to be defined in your web application. In fact, this is how you can override the default UI tags and <a shape="rect" href="form-tags.html">Form Tags</a> included with the framework.</p>

<p>In addition, you can specify a location (directory on your file system) through the <code>templatePath</code> or <code>TemplatePath</code> context variable (in  the <code>web.xml</code>. If a variable is specified, the content of the directory it points to will be searched first.</p>

<div class="confluence-information-macro confluence-information-macro-note"><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>This variable is currently NOT relative to the root of your application.</p></div></div>


<p>If a property is defined on the template with the same name as a property on the action, FreeMarker will use the property defined on the template.</p>



<h2 id="UsingFreemarkerTemplates-VariableResolution">Variable Resolution</h2>

<p>When using FreeMarker with the framework, variables are looked up in several different places, in this order:</p>
<ol><li>Built-in variables</li><li>Value stack</li><li>Action context</li><li>Request scope</li><li>Session scope</li><li>Application scope</li></ol>


<p>Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP <code>s:property</code> tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.url id="url" value="http://www.yahoo.com"/&gt;
Click &lt;a xhref="${url}"&gt;here&lt;/a&gt;!
</pre>
</div></div>
<p>The built-in variables that Struts-FreeMarker integration provides are:</p>
<div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> Name </p></th><th colspan="1" rowspan="1" class="confluenceTh"><p> Description </p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> stack </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The value stack itself, useful for calls like ${stack.findString('ognl expr')} </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> action </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The action most recently executed </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> response </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The HttpServletResponse </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> res </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Same as response </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> request </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The HttpServletRequest </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> req </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Same as request </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> session </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The HttpSession </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> application </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The ServletContext </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> base </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> The request's context path </p></td></tr></tbody></table></div>



<h2 id="UsingFreemarkerTemplates-FreeMarkerconfiguration">FreeMarker configuration</h2>

<p>To configure the FreeMarker engine, just add a file <code>freemarker.properties</code> to the classpath.  The supported properties are those that the FreeMarker Configuration object expects, see <a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/docs/pgui_config.html" rel="nofollow">FreeMarker's documentation</a> for more details.  </p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>freemarker.properties example</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">
default_encoding=ISO-8859-1
template_update_delay=5
locale=no_NO
</pre>
</div></div>


<h2 id="UsingFreemarkerTemplates-Tags">Tags</h2>


<h3 id="UsingFreemarkerTemplates-UsingStrutstags">Using Struts tags </h3>

<p>Tags distributed with Struts are automatically made available to FreeMarker templates. To use any tag add "<code>@s.</code>" in front of the tag name. Like:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Using Struts tags on FreeMarker templates</b></div><div class="codeContent panelContent pdl">
<pre class="brush: html; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.if test="printName"&gt;
    &lt;@s.property value="myBeanProperty" /&gt;
&lt;/@s.if&gt;
</pre>
</div></div>


<h3 id="UsingFreemarkerTemplates-UsingJSPtags">Using JSP tags</h3>

<p>To use JSP tags that are not part of Struts you have to:</p>

<p>1. Add JspSupportSerlvet to web.xml</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Adding JspSupportSerlvet to web.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;servlet&gt;
    &lt;servlet-name&gt;JspSupportServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.apache.struts2.views.JspSupportServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
</pre>
</div></div>

<p>2. Declare the tld on web.xml or use FreeMarker's "<code>assign</code>" directive. When using the "<code>assign</code>" directive, provide the absolute path to the tld file.</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Using JSP tags on FreeMarker templates</b></div><div class="codeContent panelContent pdl">
<pre class="brush: html; gutter: false; theme: Default" style="font-size:12px;">
&lt;#assign ex=JspTaglibs["/WEB-INF/example.tld"] /&gt;

&lt;@ex.mytag text="hello" /&gt;
</pre>
</div></div>


<h2 id="UsingFreemarkerTemplates-TipsandTricks">Tips and Tricks</h2>

<p>There are some advanced features that may be useful when building Struts applications with FreeMarker.</p>


<h3 id="UsingFreemarkerTemplates-TypeConversionandLocales">Type Conversion and Locales</h3>

<p>FreeMarker has built in support for formatting dates and numbers. The formatting rules are based on the locale associated with the action request, which is by default set in <a shape="rect" href="strutsproperties.html">struts.properties</a> but can be over-ridden using the <a shape="rect" href="i18n-interceptor.html">I18n Interceptor</a>. This is normally perfect for your needs, but it is important to remember that these formatting rules are handled by FreeMarker and not by the framework's <a shape="rect" href="type-conversion.html">Type Conversion</a> support.</p>

<p>If you want the framework to handle the formatting according to the <a shape="rect" href="type-conversion.html">Type Conversion</a> you have specified, you shouldn't use the normal ${...} syntax. Instead, you should use the <a shape="rect" href="property.html">property</a> tag. The difference is that the property tag is specifically designed to take an <a shape="rect" href="ognl.html">OGNL</a> expression, evaluate it, and then convert it to a String using any <a shape="rect" href="type-conversion.html">Type Conversion</a> rules you have specified. The normal ${...} syntax will use a FreeMarker expression language, evaluate it, and then convert it to a String using the built in formatting rules. </p>

<div class="confluence-information-macro confluence-information-macro-note"><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>The difference in how type conversion is handled under Freemarker is subtle but important to understand.</p></div></div>


<h3 id="UsingFreemarkerTemplates-StringandNonStringValuesontags">String and Non String Values on tags</h3>

<p>In FreeMarker it is incorrect to quote non string values. If a value is quoted, then an string will be passed, instead of the expected object, causing an exception. For example, the "textarea" tag expects the attributes "rows" and "cols" of type Integer:</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Do not quote non string values in tag attributes!</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
&lt;@s.textarea rows=5 cols=40 /&gt;
</pre>
</div></div>


<h3 id="UsingFreemarkerTemplates-Extending">Extending</h3>

<p>Sometimes you may with to extend the framework's FreeMarker support. For example, you might want to extend the Struts tags that come bundled with the framework.</p>

<p>To extend the Freemarker support, develop a class that extends <code>org.apache.struts2.views.freemarker.FreemarkerManager</code>, overriding methods as needed, and plugin the class through the <a shape="rect" href="strutsproperties.html">struts.properties</a>:</p>
<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: plain; gutter: false; theme: Default" style="font-size:12px;">
struts.freemarker.manager.classname = com.yourcompany.YourFreeMarkerManager
</pre>
</div></div>


<h2 id="UsingFreemarkerTemplates-FreeMarkeralternativesyntax">FreeMarker alternative syntax</h2>

<p>FreeMarker by default uses the "&lt;#directive /&gt;" syntax. FreeMarker supports an alternative syntax, where [ and ] are used instead of &lt; and &gt;. To enable the alternative syntax, add [#ftl] at the beginning of the template. The alternative syntax makes it easier to differentiate between FreeMarker directives, and JSP or HTML tags.</p>

<div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Use alternative syntax</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">
[#ftl]
&lt;html&gt;
   &lt;head&gt;FreeMarker Example&lt;/head&gt;
    
   &lt;body&gt;
       &lt;h1&gt;Alternative Syntax&lt;/h1&gt;
       [@s.if test="printName"]
          [@s.property value="myBeanProperty" /]
       [/@s.if]
   &lt;/body&gt;
&lt;/html&gt;
</pre>
</div></div>

<p><img class="emoticon emoticon-light-on" src="https://cwiki.apache.org/confluence/s/en_GB/5982/f2b47fb3d636c8bc9fd0b11c0ec6d0ae18646be7.1/_/images/icons/emoticons/lightbulb_on.png" data-emoticon-name="light-on" alt="(lightbulb)"> There are a number of <a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/editors.html" rel="nofollow">IDE plugins</a> available for FreeMarker. But, if your IDE is not on the list, then the using alternative syntax will avoid conflicts between FreeMarker and the HTML syntax highlighting provided by your IDE.</p>

<h2 id="UsingFreemarkerTemplates-FreeMarkerlinks">FreeMarker links</h2>

<ul><li><a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/" rel="nofollow">Home Page</a></li><li><a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/docs/index.html" rel="nofollow">Documentation</a></li><li><a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/editors.html" rel="nofollow">IDE Integration</a></li></ul>
</div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
